# ajax提交数据反序列化
import json

from django.db.models import Q
from django.shortcuts import render, redirect
from django.http import HttpResponse
# 导入django的分页插件包，主要实现了返回大量文章数据的分页功能
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
# 用于需要用户登录之后才能进行的操作
from django.contrib.auth.decorators import login_required

from .models import (
    Campus,
    Department,
    Association,
    DepartmentPost,
    AssociationPost,
)
from .forms import (
    AssociationForm,
    DepartmentForm,
    AssociationPostForm,
    DepartmentPostForm,
)
from .recommends import (
    get_all_recent_posts_of_associations,
    get_all_recent_posts_of_departments,
    get_all_popular_posts_of_associations,
    get_all_popular_posts_of_departments,
    get_specified_number_of_recent_posts_of_associations,
    get_specified_number_of_recent_posts_of_departments,
)
from .validations import is_valid_image
from users.models import User

# 每个页面默认数据密度
PAGE_SIZE = 5


# Create your views here.
def index_view(request):
    """部门板块主页视图，附带推送社团和部门近期文章共12篇"""
    template_name = 'departments/index.html'
    # 部门和社团近期文章推送各4篇
    department_recent_posts = get_specified_number_of_recent_posts_of_departments(number=6)
    association_recent_posts = get_specified_number_of_recent_posts_of_associations(number=6)

    context = {
        'department_recent_posts': department_recent_posts,
        'association_recent_posts': association_recent_posts,
    }

    return render(request, template_name, context=context)


def associations_view(request, campus_id):
    """社团列表页面视图，相关内容分页"""
    template_name = 'departments/associations.html'
    # 依据campus_id的值按校区分类返回结果，默认campus_id=0，此时返回全部校区的社团
    # 尝试获取搜索关键词，如果关键词存在表明这是搜索模式，既可以按照社团名称模糊查询，也可以按照校区名称模糊查询
    # 如果没有搜索关键词，则按照列表的形式返回所有的社团对象
    # 如果有搜索关键词，按照搜索关键词进行模糊搜索，以列表的形式返回结果
    # 另外，不要再添加关键词了，代码接近不可维护的边缘，如果需要其他关键词分类或者搜索，建议选取其他方式实现
    kw = request.GET.get('kw', "")
    if campus_id:
        if not kw:
            associations = Association.objects.filter(
                Q(campus__id=campus_id) & Q(status=Association.STATUS_NORMAL)
            )
        else:
            associations = Association.objects.filter(
                Q(campus__id=campus_id) & Q(status=Association.STATUS_NORMAL) & (
                        Q(name__icontains=kw) | Q(campus__name__icontains=kw))
            )
    else:
        if not kw:
            associations = Association.get_all_normal_associations()
        else:
            associations = Association.objects.filter(
                Q(status=Association.STATUS_NORMAL) & (Q(name__icontains=kw) | Q(campus__name__icontains=kw))
            )

    # 用于按校区给每页获取到的社团进行分类展示
    campuses = Campus.get_all_campuses()

    # 分页展示这些部门或社团，需要url传入关键词参数page
    # 对查询到的associations对象进行分页，每页设置为展示5条记录
    page = request.GET.get('page')
    paginator = Paginator(associations, PAGE_SIZE)

    try:
        associations = paginator.page(page)
    except PageNotAnInteger:
        # 如果给定的页数无效，展示第一页内容
        associations = paginator.page(1)
    except EmptyPage:
        # 如果给定的页数超过范围，展示最后一页内容
        associations = paginator.page(paginator.num_pages)
    # url中存在的无意义参数，强迫症表示快要哭死过去了QAQ，已经放弃抵抗
    context = {
        'associations': associations,
        'campuses': campuses,
        'kw': kw,
    }
    return render(request, template_name, context=context)


def specific_association_view(request, association_id):
    """社团详情页面视图"""
    template_name = 'departments/association_info.html'
    error_message_template_name = 'departments/error_message.html'
    association = Association.get_normal_association(association_id)
    if association:
        context = {
            'association': association,
            'is_existing': True,
        }
        return render(request, template_name, context=context)
    else:
        context = {
            'message': "此社团不存在",
            'code': "404",
        }
        return render(request, error_message_template_name, context=context)


def preview_association_view(request, association_id):
    """待审核的社团详情页面预览视图"""
    template_name = 'departments/association_info.html'
    error_message_template_name = 'departments/error_message.html'
    association = Association.get_hidden_association(association_id)
    if association:
        context = {
            'association': association,
            'is_existing': True,
        }
        return render(request, template_name, context=context)
    else:
        context = {
            'message': "待审核的社团不存在",
            'code': "404",
        }
        return render(request, error_message_template_name, context=context)


def departments_view(request, campus_id):
    """部门列表页面视图，相关内容分页"""
    template_name = 'departments/departments.html'

    kw = request.GET.get('kw', "")
    if campus_id:
        if not kw:
            departments = Department.objects.filter(
                Q(college__campus__id=campus_id) & Q(status=Department.STATUS_NORMAL)
            )
        else:
            departments = Department.objects.filter(
                Q(college__campus__id=campus_id) & Q(status=Department.STATUS_NORMAL) & (
                        Q(name__icontains=kw) | Q(college__name__icontains=kw))
            )
    else:
        if not kw:
            departments = Department.get_all_normal_departments()
        else:
            departments = Department.objects.filter(
                Q(status=Department.STATUS_NORMAL) & (Q(name__icontains=kw) | Q(college__name__icontains=kw))
            )

    campuses = Campus.get_all_campuses()
    page = request.GET.get('page')
    paginator = Paginator(departments, PAGE_SIZE)

    try:
        departments = paginator.page(page)
    except PageNotAnInteger:
        departments = paginator.page(1)
    except EmptyPage:
        departments = paginator.page(paginator.num_pages)

    context = {
        'departments': departments,
        'campuses': campuses,
        'kw': kw,
    }
    return render(request, template_name, context=context)


def specific_department_view(request, department_id):
    """部门详情页面视图"""
    template_name = 'departments/department_info.html'
    error_message_template_name = 'departments/error_message.html'
    department = Department.get_normal_department(department_id)
    if department:
        context = {
            'department': department,
            'is_existing': True,
        }
        return render(request, template_name, context=context)
    else:
        context = {
            'message': "此部门不存在",
            'code': "404",
        }
        return render(request, error_message_template_name, context=context)


def preview_department_view(request, department_id):
    """待审核部门详情页面预览视图"""
    template_name = 'departments/department_info.html'
    error_message_template_name = 'departments/error_message.html'
    department = Department.get_hidden_department(department_id)
    if department:
        context = {
            'department': department,
            'is_existing': True,
        }
        return render(request, template_name, context=context)
    else:
        context = {
            'message': "待审核的部门不存在",
            'code': "404",
        }
        return render(request, error_message_template_name, context=context)


def association_posts_view(request):
    """社团文章列表页视图"""
    template_name = 'departments/association_posts.html'
    # 不要再添加关键词了，代码接近不可维护的边缘，如果需要其他关键词分类或者搜索，建议选取其他方式实现
    kw = request.GET.get('kw', "")
    order = request.GET.get('order', "")
    if not kw:
        if order == 'recent':
            association_posts = get_all_recent_posts_of_associations()
        elif order == 'popular':
            association_posts = get_all_popular_posts_of_associations()
        else:
            association_posts = AssociationPost.get_all_normal_posts()
    else:
        if order == 'recent':
            association_posts = AssociationPost.objects.filter(
                Q(title__icontains=kw) | Q(association__name__icontains=kw)
            ).order_by('-created_time')
        elif order == 'popular':
            association_posts = AssociationPost.objects.filter(
                Q(title__icontains=kw) | Q(association__name__icontains=kw)
            ).order_by('-nov')
        else:
            association_posts = AssociationPost.objects.filter(
                Q(title__icontains=kw) | Q(association__name__icontains=kw)
            )
    # 分页展示社团文章
    page = request.GET.get('page')
    paginator = Paginator(association_posts, PAGE_SIZE)
    try:
        association_posts = paginator.page(page)
    except PageNotAnInteger:
        association_posts = paginator.page(1)
    except EmptyPage:
        association_posts = paginator.page(paginator.num_pages)

    context = {
        'association_posts': association_posts,
        'kw': kw,
        'order': order,
    }
    return render(request, template_name, context=context)


def association_specific_post_view(request, post_id):
    """社团文章详情页视图"""
    template_name = 'departments/association_post_info.html'
    error_message_template_name = 'departments/error_message.html'
    association_post = AssociationPost.get_normal_post(post_id)
    if association_post:
        # 获取到对应的文章详情信息，文章浏览量+1
        AssociationPost.nov_increased(post_id)
        content = {
            'association_post': association_post,
            'is_existing': True,
        }
        return render(request, template_name, context=content)
    else:
        context = {
            'message': "此社团文章不存在",
            'code': "404",
        }
        return render(request, error_message_template_name, context=context)


def preview_association_post_view(request, post_id):
    """社团文章草稿详情页预览视图"""
    template_name = 'departments/association_post_info.html'
    error_message_template_name = 'departments/error_message.html'
    association_post = AssociationPost.get_hidden_post(post_id)
    if association_post:
        content = {
            'association_post': association_post,
            'is_existing': True,
        }
        return render(request, template_name, context=content)
    else:
        context = {
            'message': "此社团文章草稿不存在",
            'code': "404",
        }
        return render(request, error_message_template_name, context=context)


def department_posts_view(request):
    """部门文章列表页视图"""
    template_name = 'departments/department_posts.html'

    kw = request.GET.get('kw', "")
    order = request.GET.get('order', "")
    if not kw:
        if order == 'recent':
            department_posts = get_all_recent_posts_of_departments()
        elif order == 'popular':
            department_posts = get_all_popular_posts_of_departments()
        else:
            department_posts = DepartmentPost.get_all_normal_posts()
    else:
        if order == 'recent':
            department_posts = DepartmentPost.objects.filter(
                Q(title__icontains=kw) | Q(department__name__icontains=kw)
            ).order_by('-created_time')
        elif order == 'popular':
            department_posts = DepartmentPost.objects.filter(
                Q(title__icontains=kw) | Q(department__name__icontains=kw)
            ).order_by('-nov')
        else:
            department_posts = DepartmentPost.objects.filter(
                Q(title__icontains=kw) | Q(department__name__icontains=kw)
            )

    page = request.GET.get('page')
    paginator = Paginator(department_posts, PAGE_SIZE)
    try:
        department_posts = paginator.page(page)
    except PageNotAnInteger:
        department_posts = paginator.page(1)
    except EmptyPage:
        department_posts = paginator.page(paginator.num_pages)

    context = {
        'department_posts': department_posts,
        'kw': kw,
        'order': order,
    }
    return render(request, template_name, context=context)


def department_specific_post_view(request, post_id):
    """部门文章详情页视图"""
    template_name = 'departments/department_post_info.html'
    error_message_template_name = 'departments/error_message.html'
    department_post = DepartmentPost.get_normal_post(post_id)
    if department_post:
        DepartmentPost.nov_increased(post_id)
        content = {
            'department_post': department_post,
            'is_existing': True,
        }
        return render(request, template_name, context=content)
    else:
        context = {
            'message': "此部门文章不存在",
            'code': "404",
        }
        return render(request, error_message_template_name, context=context)


def preview_department_post_view(request, post_id):
    """部门文章草稿详情页预览视图"""
    template_name = 'departments/department_post_info.html'
    error_message_template_name = 'departments/error_message.html'
    department_post = DepartmentPost.get_hidden_post(post_id)
    if department_post:
        content = {
            'department_post': department_post,
            'is_existing': True,
        }
        return render(request, template_name, context=content)
    else:
        context = {
            'message': "此部门文章草稿不存在",
            'code': "404",
        }
        return render(request, error_message_template_name, context=context)


@login_required(login_url='users:login')
def add_association_post_view(request):
    """新建社团文章视图"""
    template_name = 'departments/add_association_post.html'

    if request.method == 'POST':
        post_form = AssociationPostForm(data=request.POST)
        if post_form.is_valid():
            new_association_post = post_form.save(commit=False)
            new_association_post.author = request.user
            new_association_post.save()
            if new_association_post.status == AssociationPost.STATUS_NORMAL:
                return redirect(new_association_post)
            # 进入草稿预览模式
            return redirect('departments:preview_association_post', post_id=new_association_post.id)
        else:
            # 暂时不考虑二次验证失败，错误描述信息和用户原始提交数据展示在网页中
            errors = post_form.errors.as_json()
            context = {
                'association_post_form': post_form,
                'errors': errors,
            }
            return render(request, template_name, context=context)
    else:
        association_post_form = AssociationPostForm()
        context = {
            'association_post_form': association_post_form,
            'errors': None,
        }
        return render(request, template_name, context=context)


@login_required(login_url='users:login')
def delete_association_post_view(request, post_id):
    """删除社团文章视图"""
    error_message_template_name = 'departments/error_message.html'
    if request.method == 'POST':
        association_post = AssociationPost.get_post(post_id)
        if not association_post:
            context = {
                'message': "待删除的社团文章不存在",
                'code': "404",
            }
            return render(request, error_message_template_name, context=context)
        elif request.user != association_post.author:
            context = {
                'message': "很抱歉，您不是本文作者，没有权限删除此文章",
                'code': "403",
            }
            return render(request, error_message_template_name, context=context)
        else:
            association_post.delete()
            return redirect('departments:association_posts')
    else:
        context = {
            'message': "仅允许POST请求方式进行删除操作",
            'code': "403",
        }
        return render(request, error_message_template_name, context=context)


@login_required(login_url='users:login')
def update_association_post_view(request, post_id):
    """修改社团文章视图"""
    template_name = 'departments/update_association_post.html'
    error_message_template_name = 'departments/error_message.html'
    association_post = AssociationPost.get_post(post_id)
    if not association_post:
        context = {
            'message': "待修改的社团文章不存在",
            'code': "404",
        }
        return render(request, error_message_template_name, context=context)
    elif request.user != association_post.author:
        context = {
            'message': "很抱歉，您不是本文作者，没有权限修改此文章",
            'code': "403",
        }
        return render(request, error_message_template_name, context=context)
    elif request.method == 'POST':
        post_form = AssociationPostForm(data=request.POST, instance=association_post)
        if post_form.is_valid():
            association_post = post_form.save(commit=False)
            # 被修改过后的文章视为新文章，文章浏览量清零，点赞量清零
            association_post.self_nov_cleared()
            association_post.self_nol_cleared()
            association_post.save()
            if association_post.status == AssociationPost.STATUS_NORMAL:
                return redirect(association_post)
            # 进入草稿预览模式
            return redirect('departments:preview_association_post', post_id=post_id)
        else:
            errors = post_form.errors.as_json()
            context = {
                'association_post_form': post_form,
                'errors': errors,
            }
            return render(request, template_name, context=context)
    else:
        association_post_form = AssociationPostForm(instance=association_post)
        context = {
            'association_post_form': association_post_form,
            'errors': None,
        }
        return render(request, template_name, context=context)


@login_required(login_url='users:login')
def add_department_post_view(request):
    """新建部门文章"""
    template_name = 'departments/add_department_post.html'
    if request.method == 'POST':
        post_form = DepartmentPostForm(data=request.POST)
        if post_form.is_valid():
            new_department_post = post_form.save(commit=False)
            new_department_post.author = request.user
            new_department_post.save()
            if new_department_post.status == DepartmentPost.STATUS_NORMAL:
                return redirect(new_department_post)
            return redirect('departments:preview_department_post', post_id=new_department_post.id)
        else:
            errors = post_form.errors.as_json()
            context = {
                'department_post_form': post_form,
                'errors': errors,
            }
            return render(request, template_name, context=context)
    else:
        department_post_form = DepartmentPostForm()
        context = {
            'department_post_form': department_post_form,
            'errors': None,
        }
        return render(request, template_name, context=context)


@login_required(login_url='users:login')
def delete_department_post_view(request, post_id):
    """删除部门文章"""
    error_message_template_name = 'departments/error_message.html'
    if request.method == 'POST':
        department_post = DepartmentPost.get_post(post_id)
        if not department_post:
            context = {
                'message': "待删除的部门文章不存在",
                'code': "404",
            }
            return render(request, error_message_template_name, context=context)
        elif request.user != department_post.author:
            context = {
                'message': "很抱歉，您不是本文作者，没有权限删除此文章",
                'code': "403",
            }
            return render(request, error_message_template_name, context=context)
        else:
            department_post.delete()
            return redirect('departments:department_posts')
    else:
        context = {
            'message': "仅允许POST请求方式进行删除操作",
            'code': "403",
        }
        return render(request, error_message_template_name, context=context)


@login_required(login_url='users:login')
def update_department_post_view(request, post_id):
    """更新部门文章"""
    template_name = 'departments/update_department_post.html'
    error_message_template_name = 'departments/error_message.html'
    department_post = DepartmentPost.get_post(post_id)
    if not department_post:
        context = {
            'message': "待修改的部门文章不存在",
            'code': "404",
        }
        return render(request, error_message_template_name, context=context)
    elif request.user != department_post.author:
        context = {
            'message': "很抱歉，您不是本文作者，没有权限修改此文章",
            'code': "403",
        }
        return render(request, error_message_template_name, context=context)
    elif request.method == 'POST':
        post_form = DepartmentPostForm(data=request.POST, instance=department_post)
        if post_form.is_valid():
            department_post = post_form.save(commit=False)
            department_post.self_nov_cleared()
            department_post.self_nol_cleared()
            department_post.save()
            if department_post.status == DepartmentPost.STATUS_NORMAL:
                return redirect(department_post)
            return redirect('departments:preview_department_post', post_id=post_id)
        else:
            errors = post_form.errors.as_json()
            context = {
                'department_post_form': post_form,
                'errors': errors,
            }
            return render(request, template_name, context=context)
    else:
        department_post_form = DepartmentPostForm(instance=department_post)
        context = {
            'department_post_form': department_post_form,
            'errors': None,
        }
        return render(request, template_name, context=context)


@login_required(login_url='users:login')
def add_association_view(request):
    """成立一个新社团"""
    template_name = 'departments/add_association.html'
    if request.method == 'POST':
        form = AssociationForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            new_association = form.save(commit=False)
            new_association.owner = request.user
            new_association.save()
            return redirect('departments:preview_association', association_id=new_association.id)
        else:
            errors = form.errors
            context = {
                'association_form': form,
                'errors': errors,
            }
            return render(request, template_name, context=context)
    else:
        association_form = AssociationForm()
        context = {
            'association_form': association_form,
            'errors': None,
        }
        return render(request, template_name, context=context)


@login_required(login_url='users:login')
def delete_association_view(request, association_id):
    """解散一个已有的社团"""
    error_message_template_name = 'departments/error_message.html'
    if request.method == 'POST':
        association = Association.get_association(association_id)
        if not association:
            context = {
                'message': "待解散的社团不存在",
                'code': "404",
            }
            return render(request, error_message_template_name, context=context)
        elif association.owner != request.user:
            context = {
                'message': "很抱歉，您不是当前社团管理员，没有权限解散社团",
                'code': "403",
            }
            return render(request, error_message_template_name, context=context)
        else:
            # 清理旧的logo图片
            if association.image:
                association.image.delete()
            # 删除社团
            association.delete()
            return redirect('departments:associations', campus_id=0)
    else:
        context = {
            'message': "仅允许POST请求方式解散社团",
            'code': "403",
        }
        return render(request, error_message_template_name, context=context)


@login_required(login_url='users:login')
def update_association_view(request, association_id):
    """修改一个社团的现有信息"""
    template_name = 'departments/update_association.html'
    error_message_template_name = 'departments/error_message.html'
    association = Association.get_association(association_id)
    if not association:
        context = {
            'message': "待修改信息的社团不存在",
            'code': "404",
        }
        return render(request, error_message_template_name, context=context)
    elif request.user != association.owner:
        context = {
            'message': "很抱歉，您不是当前社团管理员，没有权限修改此社团信息",
            'code': "403",
        }
        return render(request, error_message_template_name, context=context)
    elif request.method == 'POST':
        # 在接受用户修改的内容之前，先判断用户是由有意图修改原先的logo图片
        # 如果有修改意图且原先logo存在，那就清理原先的logo图片，释放服务器存储空间
        image = request.FILES.get('image')
        if image and is_valid_image(image) and association.image:
            association.image.delete()

        form = AssociationForm(data=request.POST, files=request.FILES, instance=association)
        if form.is_valid():
            # 保存被修改后的社团信息，包含新的图片url，如果用户想要修改原先的logo图片的话
            association = form.save()
            if association.status == Association.STATUS_NORMAL:
                return redirect(association)
            return redirect('departments:preview_association', association_id=association_id)
        else:
            errors = form.errors
            context = {
                'association_form': form,
                'errors': errors,
            }
            return render(request, template_name, context=context)
    else:
        association_form = AssociationForm(instance=association)
        context = {
            'association_form': association_form,
            'errors': None,
        }
        return render(request, template_name, context=context)


@login_required(login_url='users:login')
def add_department_view(request):
    """成立一个新部门"""
    template_name = 'departments/add_department.html'
    if request.method == 'POST':
        form = DepartmentForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            new_department = form.save(commit=False)
            new_department.owner = request.user
            new_department.save()
            return redirect('departments:preview_department', department_id=new_department.id)
        else:
            errors = form.errors
            context = {
                'department_form': form,
                'errors': errors,
            }
            return render(request, template_name, context=context)
    else:
        department_form = DepartmentForm()
        context = {
            'department_form': department_form,
            'errors': None,
        }
        return render(request, template_name, context=context)


@login_required(login_url='users:login')
def delete_department_view(request, department_id):
    """解散一个已有的部门"""
    error_message_template_name = 'departments/error_message.html'
    if request.method == 'POST':
        department = Department.get_department(department_id)
        if not department:
            context = {
                'message': "待解散的部门不存在",
                'code': "404",
            }
            return render(request, error_message_template_name, context=context)
        elif request.user != department.owner:
            context = {
                'message': "很抱歉，您不是当前部门管理员，没有权限解散部门",
                'code': "403",
            }
            return render(request, error_message_template_name, context=context)
        else:
            if department.image:
                department.image.delete()
            department.delete()
            return redirect('departments:departments', campus_id=0)
    else:
        context = {
            'message': "仅允许POST请求方式解散部门",
            'code': "403",
        }
        return render(request, error_message_template_name, context=context)


@login_required(login_url='users:login')
def update_department_view(request, department_id):
    """修改一个已存在的部门信息"""
    template_name = 'departments/update_department.html'
    error_message_template_name = 'departments/error_message.html'
    department = Department.get_department(department_id)
    if not department:
        context = {
            'message': "待修改信息的部门不存在",
            'code': "404",
        }
        return render(request, error_message_template_name, context=context)
    elif department.owner != request.user:
        context = {
            'message': "很抱歉，您不是当前部门管理员，没有权限修改此部门信息",
            'code': "403",
        }
        return render(request, error_message_template_name, context=context)
    elif request.method == 'POST':
        image = request.FILES.get('image')
        if image and is_valid_image(image) and department.image:
            department.image.delete()

        form = DepartmentForm(data=request.POST, files=request.FILES, instance=department)
        if form.is_valid():
            department = form.save()
            if department.status == Department.STATUS_NORMAL:
                return redirect(department)
            return redirect('departments:preview_department', department_id=department_id)
        else:
            errors = form.errors
            context = {
                'department_form': form,
                'errors': errors,
            }
            return render(request, template_name, context=context)
    else:
        department_form = DepartmentForm(instance=department)
        context = {
            'department_form': department_form,
            'errors': None,
        }
        return render(request, template_name, context=context)


# 留到个人空间中实现吧（滑稽）
# def change_association_manager_view(request,association_id):
#     """更改社团信息管理员"""
#     # 使用post方式更改
#     pass
#
#
# def change_department_manager_view(request,department_id):
#     """更改部门信息管理员"""
#     pass


def like_association_post_view(request, post_id):
    """给社团文章点个赞，特性是无需用户登录即可点赞，不可重复点赞，可取消点赞"""
    error_message_template_name = 'departments/error_message.html'
    if request.method == 'POST':
        association_post = AssociationPost.get_normal_post(post_id)
        if association_post:
            # 点赞标记反序列化得到布尔值
            is_like = json.loads(request.POST.get('is_like'))
            if is_like:
                # 点赞标记为True
                association_post.nol_increased()
                return HttpResponse('success')
            elif association_post.nol:
                # 点赞标记为False即为取消点赞，且文章点赞数量不为0
                association_post.nol_decreased()
                return HttpResponse('success')
            else:
                # 如果点赞标记为False，且文章点赞数为0，此时不支持取消点赞
                # 主要是针对文章被修改之后，点赞数清零，过去点赞此文章用户的点赞记录作废
                return HttpResponse('failure')
        else:
            context = {
                'message': "待点赞的社团文章不存在",
                'code': "404",
            }
            return render(request, error_message_template_name, context=context)
    else:
        context = {
            'message': "仅允许POST请求方式给社团文章点赞",
            'code': "403",
        }
        return render(request, error_message_template_name, context=context)


def like_department_post_view(request, post_id):
    """给部门文章点个赞"""
    error_message_template_name = 'departments/error_message.html'
    if request.method == 'POST':
        department_post = DepartmentPost.get_normal_post(post_id)
        if department_post:
            is_like = json.loads(request.POST.get('is_like'))
            if is_like:
                department_post.nol_increased()
                return HttpResponse('success')
            elif department_post.nol:
                department_post.nol_decreased()
                return HttpResponse('success')
            else:
                return HttpResponse('failure')
        else:
            context = {
                'message': "待点赞的部门文章不存在",
                'code': "404",
            }
            return render(request, error_message_template_name, context=context)
    else:
        context = {
            'message': "仅允许POST请求方式给部门文章点赞",
            'code': "403",
        }
        return render(request, error_message_template_name, context=context)

# 呃，我怎么感觉自己只会复制粘贴，我太菜了呀！！！
# 社团、部门增删改功能大体上算是完成了吧，有亿点点细节需要处理:-(
# emmm，点赞功能好像要遥遥无期了
# 视图板块展示和编辑功能基本已完成，下一步将引入文章点赞功能
